﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace Fraktalysator.Forms
{
	class ImageDisplayForm : ToolForm
	{
		protected Panel ImageContainer { get; private set; }
		protected bool IsSettingImageContainerSize { get; private set; }

		public Image Image { get { return this.ImageContainer.BackgroundImage; } set { this.ImageContainer.BackgroundImage = value; } }
		public virtual Size ImageSize { get { return this.ImageContainer.ClientSize; } set { this.ImageContainer.ClientSize = value; } }
		public ImageLayout ImageLayout { get { return this.ImageContainer.BackgroundImageLayout; } set { this.ImageContainer.BackgroundImageLayout = value; } }

		public ImageDisplayForm()
			: base()
		{
			this.IsSettingImageContainerSize = true;
			this.ImageContainer = new Controls.DoubleBufferedPanel();
			this.ImageContainer.Location = new Point(0,0);
			this.ImageContainer.Name = "ImageContainer";
			this.ImageContainer.Margin = new Padding(0);
			this.ImageContainer.ClientSizeChanged += ImageContainer_ClientSizeChanged;
			this.ImageContainer.BackgroundImageChanged += ImageContainer_BackgroundImageChanged;
			this.Controls.Add(this.ImageContainer);
		}

		void ImageContainer_BackgroundImageChanged(object sender, EventArgs e)
		{
			this.OnImageChanged(e);
		}

		protected override void OnResize(EventArgs e)
		{
			if (ArgusLib.Environment.Runtime == ArgusLib.Runtime.Mono && this.IsSettingImageContainerSize == false)
			{
				base.OnResize(e);
				return;
			}

			if (this.ClientSize.Width != this.ImageContainer.Width + this.ImageContainer.Margin.Horizontal ||
				this.ClientSize.Height != this.ImageContainer.Height + this.ImageContainer.Margin.Vertical)
			{
				this.ImageContainer.Size = new Size(
					this.ClientSize.Width - this.ImageContainer.Margin.Horizontal,
					this.ClientSize.Height - this.ImageContainer.Margin.Vertical);
			}
			base.OnResize(e);
		}

		//protected override void OnSizeChanged(EventArgs e)
		//{
		//	if (ArgusLib.Environment.Runtime == ArgusLib.Runtime.Mono)
		//	{
		//		if (this.ClientSize.Width != this.ImageContainer.Width + this.ImageContainer.Margin.Horizontal ||
		//		this.ClientSize.Height != this.ImageContainer.Height + this.ImageContainer.Margin.Vertical)
		//		{
		//			this.ImageContainer.Size = new Size(
		//				this.ClientSize.Width - this.ImageContainer.Margin.Horizontal,
		//				this.ClientSize.Height - this.ImageContainer.Margin.Vertical);
		//		}
		//	}
		//	base.OnSizeChanged(e);
		//}

		void ImageContainer_ClientSizeChanged(object sender, EventArgs e)
		{
			this.IsSettingImageContainerSize = false;
			this.ClientSize = new Size(
				this.ImageContainer.Width + this.ImageContainer.Margin.Horizontal,
				this.ImageContainer.Height + this.ImageContainer.Margin.Vertical);
			this.IsSettingImageContainerSize = true;
		}

		protected virtual void OnImageChanged(EventArgs e)
		{
		}
	}
}
